package io.renren.utils.oss;

import com.aliyun.oss.OSSClient;
import io.renren.utils.RRException;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 阿里云存储
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-26 16:22
 */
public class AliyunCloudStorageService extends CloudStorageService{
    private OSSClient client;

    public AliyunCloudStorageService(CloudStorageConfig config){
        this.config = config;

        //初始化
        init();
    }

    private void init(){
        client = new OSSClient(config.getAliyunEndPoint(), config.getAliyunAccessKeyId(),
                config.getAliyunAccessKeySecret());
    }

    @Override
    public String upload(byte[] data, String path) {
        return upload(new ByteArrayInputStream(data), path);
    }

    @Override
    public String upload(InputStream inputStream, String path) {
        try {
          client.putObject(config.getAliyunBucketName(), path, inputStream);
//          OSSClient  client1 = new OSSClient(config.getAliyunEndPoint(),"STS.NJHcZtTcF9dr78TkpHdHKN5Tv",
//              "431MwzP9URjrj9n9fJGj82eNQqNs98gtrwM8nUFaqYpJ","CAISlgJ1q6Ft5B2yfSjIr4n9KODAubxnjqaZNR7lj3AdaMdnofD/lDz2IH5Nf3FrAOofsPo+mmxX7v8alqVoRoReREvCKM1565kPQNEvxAiF6aKP9rUhpMCPKwr6UmzGvqL7Z+H+U6mqGJOEYEzFkSle2KbzcS7YMXWuLZyOj+wIDLkQRRLqL0AFZrFsKxBltdUROFbIKP+pKWSKuGfLC1dysQcO7gEa4K+kkMqH8Uic3h+oiM1t/t6hesT+Npc9bc0uA4zrhtYbLPSRjHRijDFR77pzgaB+/jPKg8qQGVE54W/dbbOPqoY2dlYnOPlkR/Ec9aDm5vR8u/3OkYPs0ApKPuxTXCnZSZqp3MzeAuStO8br1WxXxY0GuBqAATXgtQD7nJt/pDsZZnqsKQD8FqDHk4YjjZtRwVdLGL7p5x+7efTs32mzM+Nzo4aN9fC0Ppl/ROCL8Z6oNO5Jp7Wiy1rC+Vv6jmRpWyLIh5riGPBAF89HA1+HfrlvWKI3UDN63zHGpB+WqDW+tyrC9fK6x3Ub0dzoQdxvmBf7CR/f");
//          client1.putObject(config.getAliyunBucketName(), path, inputStream);
        } catch (Exception e){
            e.printStackTrace();
            throw new RRException("上传文件失败，请检查配置信息", e);
        }

        return config.getAliyunDomain() + "/" + path;
    }

    @Override
    public String upload(byte[] data) {
        return upload(data, getPath(config.getAliyunPrefix()));
    }

    @Override
    public String upload(InputStream inputStream) {
        return upload(inputStream, getPath(config.getAliyunPrefix()));
    }
}
